<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\property\users;

use app\common\model\BaseModel;
use app\common\model\property\auth\PropertyAuthAccessModel;
use app\common\model\property\group\PropertyGroupModel;
use app\common\model\property\users\PropertyUsersLogModel;
use app\common\model\users\UsersModel;

class PropertyUsersModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'property_users';
    }

    public function userInfo()
    {
        return $this->hasOne(UsersModel::class, 'id', 'user_id');
    }


    public function groupInfo()
    {
        return $this->hasOne(PropertyGroupModel::class, 'id', 'group_id');
    }

    public function getRoleIdsAttr()
    {
        return PropertyAuthAccessModel::where('items_uid', $this->id)->column('role_id');
    }

    public function getLastLoginTimeAttr()
    {
        return PropertyUsersLogModel::where('user_id', $this->user_id)
            ->where('items_id', $this->items_id)
            ->where('log_type', 'LOGIN')
            ->order('id', 'desc')
            ->value('add_time');
    }

    public function getUsernameAttr()
    {
        return UsersModel::where('id', $this->user_id)->value('username');
    }

}
